package 链表题目;

import java.util.PriorityQueue;

//有序矩阵中第K小的元素
//https://leetcode.cn/problems/kth-smallest-element-in-a-sorted-matrix/description/
class 查找有序矩阵中第K小的元素 {
    public int kthSmallest(int[][] matrix, int k) {
        //优先队列
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        int i = 0;
        while (i < matrix.length) {
            for (int j = 0; j < matrix[i].length; j++) {
                queue.add(matrix[i][j]);
            }
            i++;
        }
        int ans = 0;
        while (k != 0) {
            ans = queue.poll();
            k--;
        }
        return ans;
    }


//    public static void main(String[] args) {
//        System.out.println(Integer.MAX_VALUE);
//        new 查找有序矩阵中第K小的元素().kthSmallest(new int[][]{{1, 5, 9}, {10, 11, 13}, {12, 13, 15}}, 8);
//    }
}
